package org.zhouhe.tag.hotinfo;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.zhouhe.modules.hotinfo.service.HotInfoService;
import org.zhouhe.vo.HotInfo;

public class HotInfoList extends TagSupport {

	private static final long serialVersionUID = 3886850494335003326L;
	private ServletContext servletContext;
	private WebApplicationContext wac;
	private HotInfoService hotInfoService;

	private Integer typeId;
	private Integer height;
	private Integer resultSize = 10;
	private String title;
	private String className;
	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	public int doStartTag() {
		String style = "";
		if(title == null || title.trim().equals("")){
			title = "热点动态";
		}
		if(height != null){
			style = "height: "+ height +"px; overflow-y: hidden;";
		}
		try {
			StringBuilder html = new StringBuilder();
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			servletContext = request.getSession().getServletContext();
			wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
			hotInfoService = (HotInfoService) wac.getBean("hotInfoService");
			
			List<HotInfo> hotInfoList = hotInfoService.getHotInfoByType(typeId, resultSize);
			
			html.append("<div class='"+ className +"'>");
			html.append("  <ul class='blockTag-title-ul'>");
			html.append("    <li class='blockTag-title-text'>"+ title +"</li>");
			html.append("    <li style='float:right'><a target='_blank' href='hotinfoList.action'>更多&gt;&gt;</a></li>");
			html.append("  </ul>");
			html.append("");
			html.append("  <div class='blockTag-content-div' style='"+ style +"'>");
			html.append("    <marquee direction='up' scrollamount='1' scrolldelay='80' onmouseover='this.stop()' onmouseout='this.start();'>  <ul class='hot-info-ul'>");
			if (hotInfoList.size() > 0) {
				for (HotInfo info : hotInfoList) {
					html.append("      <li>" + info.getTitle() + "<span style='color: #999999;'>("+ sdf.format(info.getCreateTime()) +")</span></li>");
				}
			}else{
				html.append("      <li style='border:0px;text-align:center;list-style:none'>暂时没有数据！</li>");
			}
			html.append("      </ul></marquee>");
			html.append("  </div>");
			html.append("  <div style='clear:both;'></div>");
			html.append("</div>");
			html.append("");
			html.append("");
			html.append("");
			JspWriter out = pageContext.getOut();
			out.print(html.toString());
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	public void setResultSize(Integer resultSize) {
		this.resultSize = resultSize;
	}

	public Integer getResultSize() {
		return resultSize;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getClassName() {
		return className;
	}

	public void setTypeId(Integer typeId) {
		this.typeId = typeId;
	}

	public Integer getTypeId() {
		return typeId;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	public Integer getHeight() {
		return height;
	}

}
